# Maintainer: Saul Ibarra Corretge <saghul@gmail.com>

_realname=pyqt4
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-common"
         "${MINGW_PACKAGE_PREFIX}-python2-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=4.11.4
pkgrel=3
pkgdesc="Qt4 bindings for Python (mingw-w64)"
arch=('any')
license=('GPL')
url="http://www.riverbankcomputing.co.uk/software/pyqt"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-sip"
             "${MINGW_PACKAGE_PREFIX}-python3-sip"
             "${MINGW_PACKAGE_PREFIX}-qt4")
options=('strip' 'staticlibs')
source=("https://downloads.sourceforge.net/project/pyqt/PyQt4/PyQt-$pkgver/PyQt-win-gpl-${pkgver}.zip"
        001-mingw-python.patch)
sha256sums=('5a5c5b23b9fba85bcae44cd91e524b267d40e18966a66de72842f4d96832ad29'
            '1effff11ec3e558d724d75dab529f130bf2fad8f71ba274813a094242d94957e')

prepare() {
  cd "${srcdir}"/PyQt-win-gpl-${pkgver}

  # Apply patches
  patch -p0 -i "${srcdir}"/001-mingw-python.patch
}

build() {
  for builddir in python{2,3}-${MINGW_CHOST}; do
    [[ -d ${builddir} ]] && rm -rf ${builddir}
    cp -r PyQt-win-gpl-${pkgver} ${builddir}
    pushd ${builddir}
    [[ "${builddir%-${MINGW_CHOST}}" == "python2" ]] && {
      local _sipdir=Py2-Qt4
    } || {
      local _sipdir=PyQt4
    }
    ${MINGW_PREFIX}/bin/${builddir%-${MINGW_CHOST}} configure-ng.py \
      --confirm-license \
      --qsci-api \
      --no-designer-plugin \
      --sipdir ${MINGW_PREFIX}/share/sip/${_sipdir} \
      -q ${MINGW_PREFIX}/bin/qmake.exe \
      --verbose
    make V=1
    popd
  done
}

package_pyqt4-common(){
  pkgdesc="Common PyQt files shared between python3-pyqt4 and python2-pyqt4"
  depends=("${MINGW_PACKAGE_PREFIX}-qt4")

  cd python2-${MINGW_CHOST}
  make -C pyrcc DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install
  make -C pylupdate DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install

  install -Dm644 PyQt4.api "${pkgdir}${MINGW_PREFIX}"/share/qt4/qsci/api/python/PyQt4.api
}

package_python3-pyqt4(){
  pkgdesc="A set of Python 3.x bindings for the Qt4 toolkit"
  depends=("${MINGW_PACKAGE_PREFIX}-python3-sip" "${MINGW_PACKAGE_PREFIX}-pyqt4-common" "${MINGW_PACKAGE_PREFIX}-python3")

  cd python3-${MINGW_CHOST}
  # INSTALL_ROOT is needed for the QtDesigner module, the other Makefiles use DESTDIR
  make DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install

  local _py3basever=$(${MINGW_PREFIX}/bin/python3 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  # Provided by pyqt-common
  rm "${pkgdir}${MINGW_PREFIX}"/bin/{pylupdate4,pyrcc4}.exe
  rm -rf "${pkgdir}${MINGW_PREFIX}"/qsci
  rm -rf "${pkgdir}${MINGW_PREFIX}"/share/qt4/qsci

  # Remove Python 2 uic port
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/python${_py3basever}/site-packages/PyQt4/uic/port_v2
}

package_python2-pyqt4(){
  pkgdesc="A set of Python 2.x bindings for the Qt4 toolkit"
  depends=("${MINGW_PACKAGE_PREFIX}-python2-sip" "${MINGW_PACKAGE_PREFIX}-pyqt4-common" "${MINGW_PACKAGE_PREFIX}-python2")

  cd python2-${MINGW_CHOST}
  # INSTALL_ROOT is needed for the QtDesigner module, the other Makefiles use DESTDIR
  make DESTDIR="${pkgdir}" INSTALL_ROOT="${pkgdir}" install

  local _py2basever=$(${MINGW_PREFIX}/bin/python2 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  # Fix conflicts
  mv "${pkgdir}${MINGW_PREFIX}"/bin/{,python2-}pyuic4.bat
  rm "${pkgdir}${MINGW_PREFIX}"/bin/{pylupdate4,pyrcc4}.exe
  rm -rf "${pkgdir}${MINGW_PREFIX}"/qsci
  rm -rf "${pkgdir}${MINGW_PREFIX}"/share/qt4/qsci

  # Remove Python 3 uic port
  rm -rf "${pkgdir}${MINGW_PREFIX}"/lib/python${_py2basever}/site-packages/PyQt4/uic/port_v3
}

# Wrappers
package_mingw-w64-i686-pyqt4-common() {
  package_pyqt4-common
}

package_mingw-w64-i686-python2-pyqt4() {
  package_python2-pyqt4
}

package_mingw-w64-i686-python3-pyqt4() {
  package_python3-pyqt4
}

package_mingw-w64-x86_64-pyqt4-common() {
  package_pyqt4-common
}

package_mingw-w64-x86_64-python2-pyqt4() {
  package_python2-pyqt4
}

package_mingw-w64-x86_64-python3-pyqt4() {
  package_python3-pyqt4
}

